package com.opengroupe.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.opengroupe.R;
import com.opengroupe.dto.ReponseDto;

public class QuestionArrayAdapter extends ArrayAdapter<ReponseDto>{
	private List<ReponseDto> reponses;
    private Context context;
	private int layoutResourceId;
	private List<String> selectedIdReponses;
	private String TAG;

    public QuestionArrayAdapter(Context context, int resourceLayoutId, List<ReponseDto> reponses) {
        super(context, resourceLayoutId, reponses);
        layoutResourceId = resourceLayoutId;
        this.context = context;
        this.reponses = (ArrayList<ReponseDto>) reponses;
        TAG = context.getString(R.string.log_prefix);
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ReponseHolder holder;
        
        if (row == null) {
        	LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new ReponseHolder();
            holder.itemReponse = (LinearLayout) row.findViewById(com.opengroupe.R.id.itemReponse);
            holder.txtReponse = (TextView) row.findViewById(com.opengroupe.R.id.txtReponse);
            holder.chkReponse = (CheckBox) row.findViewById(com.opengroupe.R.id.chkReponse);
            holder.txtIdReponse = (TextView) row.findViewById(com.opengroupe.R.id.txtIdReponse);
            
            holder.itemReponse.setTag(holder);
            holder.chkReponse.setTag(holder);
            row.setTag(holder);
        } else {
        	holder = (ReponseHolder)row.getTag();
        }
        
        ReponseDto reponse = reponses.get(position);
        holder.txtReponse.setText(reponse.getContenuReponse());
        holder.txtIdReponse.setText(Integer.toString(reponse.getIdReponse()));
        holder.chkReponse.setOnClickListener(chkListener);
        holder.itemReponse.setOnClickListener(itemListener);

        return row;
    }
    
    @Override
    public ReponseDto getItem(int position) {
    	return reponses.get(position);
    }
        
    static class ReponseHolder
    {
        CheckBox chkReponse;
        TextView txtReponse;
        TextView txtIdReponse;
        LinearLayout itemReponse;
    }
    
    private OnClickListener chkListener = new OnClickListener() {
		
		public void onClick(View v) {
			if(selectedIdReponses == null) {
				selectedIdReponses = new ArrayList<String>();
			}
			
			CheckBox chk = (CheckBox)v;
			ReponseHolder reponse = (ReponseHolder)chk.getTag();
			
			if(chk.isChecked()) {
				selectedIdReponses.add(reponse.txtIdReponse.getText().toString());
			} else {
				if(selectedIdReponses.contains(reponse.txtIdReponse.getText().toString())) {
					selectedIdReponses.remove(reponse.txtIdReponse.getText().toString());
				}
			}
			Log.d(TAG + " selectedIdReponses.size() = ", Integer.toString(selectedIdReponses.size()));
		}
	};
	
	private OnClickListener itemListener = new OnClickListener() {
		
		public void onClick(View v) {
			if(selectedIdReponses == null) {
				selectedIdReponses = new ArrayList<String>();
			}
			
			LinearLayout itemReponse = (LinearLayout) v;
			ReponseHolder reponse = (ReponseHolder)itemReponse.getTag();
			
			if(!reponse.chkReponse.isChecked()) {
				selectedIdReponses.add(reponse.txtIdReponse.getText().toString());
				reponse.chkReponse.setChecked(true);
			} else {
				if(selectedIdReponses.contains(reponse.txtIdReponse.getText().toString())) {
					selectedIdReponses.remove(reponse.txtIdReponse.getText().toString());
				}
				reponse.chkReponse.setChecked(false);
			}
			Log.d(TAG + " selectedIdReponses.size() = ", Integer.toString(selectedIdReponses.size()));
		}
	};
	
	public List<String> getSelectedIdReponses() {
		return selectedIdReponses;
	}
}
